<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>登陆:select 成功登陆-存入cookie</title>
</head>

<body>
    <h1>登陆:select 成功登陆-存入cookie</h1>
    用户名：<input type="text" autocomplete="off" name="" id="name"><span id="inf"></span><br>
    密码：<input type="password" id="psw"><br>
    <input type="button" value="登陆" id="loginbtn">
</body>
<script src="myquery.js"></script>
<script>
    (function () {
        /*
            1.登陆:select 成功登陆-存入cookie
        */
        let name = document.querySelector('#name');
        let inf = document.querySelector('#inf');
        let loginbtn = document.querySelector('#loginbtn');
        let psw = document.querySelector('#psw');
        let usname = location.search.slice(1);//获取用户名显示
        // console.log(usname);
        name.value = usname;
        psw.focus();//聚焦

        loginbtn.onclick = () => {

            let username = name.value.trim();
            let password = psw.value.trim();
            if (username && password) {//注册
                ajax({
                    type: 'post',
                    url: 'api/login.php',
                    data: {
                        username: username,
                        password: password
                    },
                    success: str => {
                        // console.log(str);
                        //获取cookie判断是否是登陆的，如果没有登陆才可以存cookie
                        let nnn = getCookie('username');
                        if (nnn) {
                            //拿到了：已经登陆，不允许登陆
                            alert('您已经登陆，请退出账号');
                        } else {
                            //可以登陆
                            setCookie('username', username, 7);
                        }

                    }
                });
            } else {
                inf.innerHTML = '请输入用户名和密码';
                inf.style.color = 'red';
            }
        }
    })();
</script>

</html>